package com.iris.live.services.services.kpi;

import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Properties;

/**
 * Created by Xin on 8/25/2016.
 */
@Component
public class KPIInitializer {

    private KPIConfig config;

    @Autowired
    public void KPISqlInitializer(Environment env) {
        try {
            Resource resource = new ClassPathResource("sql.properties");
            Properties props = PropertiesLoaderUtils.loadProperties(resource);
            this.config = new KPIConfig();
            this.config.setSqlList(Maps.newHashMap());
            for (String name : props.stringPropertyNames()) {
                if (name.matches("^imgt\\.kpi\\.\\w*\\.name$")) {
                    this.config.getSqlList().put(props.getProperty(name), props.getProperty(name.replace("name", "base")));
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public KPIConfig getConfig() {
        return config;
    }

    public void setConfig(KPIConfig config) {
        this.config = config;
    }
}
